package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.skyeye.event.entity.DbAiSceneCamera;
import com.fivefu.core.skyeye.event.entity.DbAiSceneNode;
import com.fivefu.core.skyeye.event.mapper.DbAiSceneNodeMapper;
import com.fivefu.core.skyeye.event.services.DbAiSceneCameraService;
import com.fivefu.core.skyeye.event.services.DbAiSceneNodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.skyeye.event.vo.SceneNodeBean;
import com.fivefu.core.skyeye.event.vo.SceneNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 机器节点表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-08-24 19:04:22
 */
@Service
public class DbAiSceneNodeServiceImpl extends ServiceImpl<DbAiSceneNodeMapper, DbAiSceneNode> implements DbAiSceneNodeService {

    @Autowired
    private DbAiSceneCameraService dbAiSceneCameraService;
    /**
     * 编辑机器节点
     * @param sceneNodeVo 节点信息
     * @param userid 操作用户编号
     */
    @Override
    public void editSceneNode(SceneNodeVo sceneNodeVo, String userid) {
        DbAiSceneNode sceneNode = new DbAiSceneNode();
        sceneNode.setUpdatedBy(userid);
        sceneNode.setUpdatedTime(LocalDateTime.now());
        sceneNode.setIsDelete(0);
        sceneNode.setNodeType(sceneNodeVo.getNodeType());//机器节点类型(slice:切片,event:识别)
        sceneNode.setNodeName(sceneNodeVo.getNodeName());//机器节点
        sceneNode.setNodeCount(sceneNodeVo.getNodeCount());//最大数量
        if (sceneNodeVo.getId()==null){//新增
            sceneNode.setCreatedBy(userid);
            sceneNode.setCreatedTime(LocalDateTime.now());
            baseMapper.insert(sceneNode);
        }else {//修改
            sceneNode.setId(sceneNodeVo.getId());
            baseMapper.updateById(sceneNode);
        }
    }

    /**
     * 删除机器节点
     * @param id 机器节点编号
     * @param userid 用户编号
     */
    @Override
    public void deleteSceneNode(Long id, String userid) {
        DbAiSceneNode sceneNode = new DbAiSceneNode();
        sceneNode.setId(id);
        sceneNode.setUpdatedTime(LocalDateTime.now());
        sceneNode.setUpdatedBy(userid);
        sceneNode.setIsDelete(1);
        baseMapper.updateById(sceneNode);
    }

    /**
     * 通过机器编号查询机器信息
     * @param id
     * @return
     */
    @Override
    public DbAiSceneNode querySceneNodeById(Long id){
        DbAiSceneNode sceneNode = baseMapper.selectById(id);
        return sceneNode;
    }


    /**
     * 根据机器节点编号获得机器节点详情
     * @param id 机器节点编号
     * @return
     */
    @Override
    public Map<String, Object> querySceneNodeDetail(Long id) {
        DbAiSceneNode sceneNode = querySceneNodeById(id);
        if (sceneNode != null){
            Map<String, Object> map = formatSceneNode(sceneNode);
            return map;
        }
        return null;
    }

    /**
     * (分页)获取机器节点列表
     * @param sceneNodeBean
     * @return
     */
    @Override
    public Page<Map<String, Object>> querySceneNodeByPage(SceneNodeBean sceneNodeBean) {
        Page<Map<String,Object>> backPage = new Page<>();
        List<Map<String,Object>> backList = new ArrayList<>();
        List<DbAiSceneNode> list = new ArrayList<>();
        if (sceneNodeBean.getPage() != null && sceneNodeBean.getLimit() != null){
            Page<DbAiSceneNode> iPage = new Page<>(sceneNodeBean.getPage(), sceneNodeBean.getLimit());
            iPage = baseMapper.querySceneNode(iPage,sceneNodeBean);
            list=iPage.getRecords();
            backPage.setTotal(iPage.getTotal());
        }else {
            list = baseMapper.querySceneNode(sceneNodeBean);
            backPage.setTotal(list.size());
        }
        list.forEach(sceneNode ->{
            Map<String, Object> map = formatSceneNode(sceneNode);
            backList.add(map);
        });
        backPage.setRecords(backList);
        return backPage;
    }

    /**
     * 根据机器节点编号查询机器节点数量
     * @param id
     * @return
     */
    @Override
    public Integer querySceneNodeCount(Long id) {
        QueryWrapper<DbAiSceneNode> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("id",id);
        DbAiSceneNode dbAiSceneNode = baseMapper.selectOne(wrapper);
        return dbAiSceneNode.getNodeCount();
    }

    /**
     * 格式化机器节点信息
     * @param sceneNode
     * @return
     */
    public Map<String, Object> formatSceneNode(DbAiSceneNode sceneNode){
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(sceneNode, SerializerFeature.WriteMapNullValue), Map.class);
        //机器节点类型(slice:切片,event:识别)
        if (sceneNode.getNodeType()!=null){
            if (sceneNode.getNodeType().equals("slice")){
                map.put("nodeTypeName","切片");
            }else if (sceneNode.getNodeType().equals("event")){
                map.put("nodeTypeName","识别");
            }
        }
        if (sceneNode.getCreatedTime() != null){
            String time = DateUtils.format(sceneNode.getCreatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("createdTime", time);
        }
        if (sceneNode.getUpdatedTime() != null){
            String time = DateUtils.format(sceneNode.getUpdatedTime(),DateUtils.DEFAULT_DATETIME_PATTERN);
            map.put("updatedTime", time);
        }
        return map;
    }

    /**
     * 查询所有的切片信息
     * @return
     */
    @Override
    public List<DbAiSceneNode> getAllSlice() {
        QueryWrapper<DbAiSceneNode> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("node_type","slice");
        List<DbAiSceneNode> resultList = baseMapper.selectList(wrapper);
        return resultList;
    }

    /**
     * 查询所有的识别信息
     * @return
     */
    @Override
    public List<DbAiSceneNode> getAllEvent() {
        QueryWrapper<DbAiSceneNode> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("node_type","event");
        List<DbAiSceneNode> resultList = baseMapper.selectList(wrapper);
        return resultList;
    }

    @Override
    public Map<String, Object> querySceneNodeByCode(String cameraCode) {
        DbAiSceneCamera dbAiSceneCamera = dbAiSceneCameraService.querySliceNodeByCamera(cameraCode);
        Map<String, Object> map = new HashMap<>();
        if (dbAiSceneCamera != null){
            List<Long> sliceId = new ArrayList<>();
            Long sliceNode = dbAiSceneCamera.getSliceNode();
            sliceId.add(sliceNode);
            map.put("slice",sliceId);
        }else {
            List<DbAiSceneNode> allSlice = getAllSlice();
            List<Long> sliceId = allSlice.stream().map(k -> k.getId()).collect(Collectors.toList());
            map.put("slice",sliceId);
        }
        List<DbAiSceneNode> allEvent = getAllEvent();
        List<Long> eventId = allEvent.stream().map(k -> k.getId()).collect(Collectors.toList());
        map.put("event",eventId);
        return map;
    }

}
